package cn.hallele.leinsteining.mc.infrastructure.util.net;

import lombok.extern.slf4j.Slf4j;
import org.lionsoul.ip2region.xdb.Searcher;
import org.springframework.util.FileCopyUtils;

import java.io.FileInputStream;
import java.io.InputStream;

/**
 * IpUtil
 *
 * @author anle5
 * @since 2024/8/12 23:47
 */
@Slf4j
public class IpUtil {
    private static String initIp2Region(String ip) {
        try (InputStream inputStream = new FileInputStream("F:\\gitee\\ip2region\\maker\\java\\target\\ip2region.xdb")) {
            byte[] bytes = FileCopyUtils.copyToByteArray(inputStream);
            Searcher searcher = Searcher.newWithBuffer(bytes);
            // searchIpInfo 的数据格式： 国家|区域|省份|城市|ISP
            return searcher.search(ip);
        } catch (Exception exception) {
            log.error(exception.getMessage(), exception);
        }
        return "";
    }

    public static void main(String[] args) {
        System.out.println(initIp2Region("119.127.198.119"));
    }
}
